//
//  MainTabBar.swift
//  sinaWeibo
//
//  Created by apple on 15/11/10.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {


    //默认构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    
    //得写 init(frame: CGRect) 系统会默认这个控件用代码创建
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    private func setupUI(){
    //添加+号按钮
        addSubview(composeBtn)
    
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
//        print(subviews)
        //手动修所有按钮位置
        let w = self.bounds.width / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        var index: CGFloat = 0
        //遍历 所有子控件
        for subview in subviews{
        
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                subview.frame = CGRectOffset(rect, index * w, 0)
                
                //空出中间位置
                if index == 1 {
                    index++
                }
                    index++
                
                
//                index += index == 1 ? 2 : 1
            
            }
       
        }
        
        composeBtn.frame = CGRectOffset(rect, w * 2, 0)
        bringSubviewToFront(composeBtn)
        
    }
    
    
    //懒加载 +号按钮 composeBtn
    lazy var composeBtn: UIButton = {
    
        let but = UIButton()
        
        but.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        but.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        but.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        but.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //系统自动布局
        but.sizeToFit()
        
        return but
    }()
  
}
